package cn.smvc.task.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.smvc.common.utils.StringUtils;
import cn.smvc.task.dao.TTimedTaskLogDao;
import cn.smvc.task.entity.TTimedTaskLog;
import cn.smvc.task.service.TTimedTaskLogService;

@Service
public class TTimedTaskLogServiceImpl implements TTimedTaskLogService{
	
	@Autowired
	private TTimedTaskLogDao tTimedTaskLogDao;

	@Override
	public int save(TTimedTaskLog taskLog) {
		int result = 0;
		try {
			result = tTimedTaskLogDao.save(taskLog);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 分页查询
	 * @param param
	 * @return
	 */
	@Override
	public Map<String, Object> queryPage(Map<String, String[]> param) {
		int page = Integer.parseInt(param.get("page")[0].toString());
		int limit = Integer.parseInt(param.get("limit")[0].toString());
		String taskId = param.get("key[taskId]") == null ? "" : param.get("key[taskId]")[0].toString();
		String taskGroup = param.get("key[taskGroup]") == null ? "" : param.get("key[taskGroup]")[0].toString();
		String exeResult = param.get("key[exeResult]") == null ? "" : param.get("key[exeResult]")[0].toString();
		String start = param.get("key[start]") == null ? "" : param.get("key[start]")[0].toString();
		String end = param.get("key[end]") == null ? "" : param.get("key[end]")[0].toString();
		
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		try {
			StringBuilder where = new StringBuilder();
			where.append("1=1 ");
			if(StringUtils.isNotEmpty(taskId)) {
				where.append("and id='").append(taskId).append("' ");
			}
			if(StringUtils.isNotEmpty(taskGroup)) {
				where.append("and task_group='").append(taskGroup).append("' ");
			}
			if(StringUtils.isNotEmpty(exeResult)) {
				where.append("and exe_result=").append(exeResult).append(" ");
			}
			String orderBy = "create_time desc ";
			List<TTimedTaskLog> list = tTimedTaskLogDao.findPage(where.toString(), page, limit, orderBy);
			count = tTimedTaskLogDao.count(where.toString());
			map.put("code", 0);
			map.put("msg", "查询成功！");
			map.put("data", list);
			map.put("count", count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return map;
	}

}
